# coding: utf-8

# A class is a collection of related methods that operate on the state of an object. An
# object’s state is held by its instance variables: variables whose names begin with @ and
# whose values are specific to that particular object.

class Sequence
	# 支持掺入
	include Enumerable

	# 构造方法
	def initialize(from, to , by)
		@from, @to, @by = from, to, by
	end

	# 掺入的模块需要的方法
	def each
		x = @from
		while x <= @to
			yield x

			x += @by
		end
	end

	# 一般方法
	def length
		return 0 if @from > @to

		return Integer((@to - @from) / @by) + 1
	end

	# 方法别名
	alias size length

	# 数组运算符重载
	def [](index)
		return nil if index < 0
		
		value = @from + @by * index
		return nil if value > @to

		return value
	end

	# 算术运算符重载
	def +(offset)
		return Sequence.new @form + offset, @to + offset, @by
	end
end

s = Sequence.new 1, 10, 2
s.each {|x| puts x}
puts s[s.size - 1]